1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.cache;
18
19 import static org.easymock.EasyMock.createMock;
20 import static org.easymock.EasyMock.expect;
21 import static org.easymock.EasyMock.replay;
22 import static org.easymock.EasyMock.verify;
23
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ImmutableMap;
26
27 import junit.framework.TestCase;
28
29 import java.util.concurrent.ExecutionException;
30
31
32
33
34
35
36 public class ForwardingCacheTest extends TestCase {
37 private Cache<String, Boolean> forward;
38 private Cache<String, Boolean> mock;
39
40 @SuppressWarnings("unchecked")
41 @Override public void setUp() throws Exception {
42 super.setUp();
43
44
45
46
47
48 mock = createMock(Cache.class);
49 forward = new ForwardingCache<String, Boolean>() {
50 @Override protected Cache<String, Boolean> delegate() {
51 return mock;
52 }
53 };
54 }
55
56 public void testGetIfPresent() throws ExecutionException {
57 expect(mock.getIfPresent("key")).andReturn(Boolean.TRUE);
58 replay(mock);
59 assertSame(Boolean.TRUE, forward.getIfPresent("key"));
60 verify(mock);
61 }
62
63 public void testGetAllPresent() throws ExecutionException {
64 expect(mock.getAllPresent(ImmutableList.of("key")))
65 .andReturn(ImmutableMap.of("key", Boolean.TRUE));
66 replay(mock);
67 assertEquals(ImmutableMap.of("key", Boolean.TRUE),
68 forward.getAllPresent(ImmutableList.of("key")));
69 verify(mock);
70 }
71
72 public void testInvalidate() {
73 mock.invalidate("key");
74 replay(mock);
75 forward.invalidate("key");
76 verify(mock);
77 }
78
79 public void testInvalidateAllIterable() {
80 mock.invalidateAll(ImmutableList.of("key"));
81 replay(mock);
82 forward.invalidateAll(ImmutableList.of("key"));
83 verify(mock);
84 }
85
86 public void testInvalidateAll() {
87 mock.invalidateAll();
88 replay(mock);
89 forward.invalidateAll();
90 verify(mock);
91 }
92
93 public void testSize() {
94 expect(mock.size()).andReturn(0L);
95 replay(mock);
96 forward.size();
97 verify(mock);
98 }
99
100 public void testStats() {
101 expect(mock.stats()).andReturn(null);
102 replay(mock);
103 assertNull(forward.stats());
104 verify(mock);
105 }
106
107 public void testAsMap() {
108 expect(mock.asMap()).andReturn(null);
109 replay(mock);
110 assertNull(forward.asMap());
111 verify(mock);
112 }
113
114 public void testCleanUp() {
115 mock.cleanUp();
116 replay(mock);
117 forward.cleanUp();
118 verify(mock);
119 }
120
121
122
123
124 private static class OnlyGet<K, V> extends ForwardingCache<K, V> {
125 @Override
126 protected Cache<K, V> delegate() {
127 return null;
128 }
129 }
130 }